package 力扣日常刷题.木23一月.第17天317;

import java.util.Stack;

/**
 * @author 帅小伙
 * @date 2022/3/17
 * @description
 */
public class Demo88字符串解码 {

    public String decodeString(String s) {
        int multi = 0;
        StringBuilder res = new StringBuilder();

        Stack<Integer> stackMulti = new Stack();
        Stack<StringBuilder> stackRes = new Stack();

        for (char c : s.toCharArray()) {
            if(c == '['){
                stackMulti.push(multi);
                stackRes.push(res);
                res = new StringBuilder();
                multi = 0;
            }else if(c == ']'){
                int pop = stackMulti.pop();
                StringBuilder t = new StringBuilder();
                for (int i = 0; i < pop; i++) {
                    t.append(res);
                }
                res = new StringBuilder(stackRes.pop()).append(t);
            }else if(c >= '0' && c <= '9'){
                multi = multi * 10 + c - '0';
            }else{
                res.append(c);
            }
        }
        return res.toString();
    }

}
